package com.wisemoresystem.seal.action.seal.type.config;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_seal_type;
import com.wisemoresystem.seal.model.t_seal_type_config;
import com.wisemoresystem.seal.service.SealTypeConfigService;

/**
 * 印章种类管理
 * 
 * @author Administrator
 *
 */
@Controller("sealType")
@Scope("prototype")
@RequestMapping("/ix/seal/type")
public class SealTypeAction extends ProjBaseAction {
	@Autowired
	private SealTypeConfigService sealTypeConfigService;

	/**
	 * 印章种类列表
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {

		List<Map<String, Object>> list = sealTypeConfigService.selectSealType();

		ModelAndView mv = new ModelAndView("/jsp/seal/type/list");
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 添加页面
	 * 
	 * @return
	 */
	@RequestMapping("/add_form")
	public ModelAndView add_form() {
		List<Map<String, Object>> list = sealTypeConfigService
				.selectSealTypeConfig();

		ModelAndView mv = new ModelAndView("/jsp/seal/type/add_form");
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 印章种类添加
	 * 
	 * @param seal_type
	 *            印章种类添加
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(t_seal_type seal_type) {

		oc.saveObj(seal_type);
		logT.logH("添加印章种类", t_seal_type.class, seal_type.get_id());

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 要修改的种类信息
	 * 
	 * @param seal_type_id
	 *            印章种类id
	 * @return
	 */
	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String seal_type_id) {
		List<Map<String, Object>> list = sealTypeConfigService
				.selectSealTypeConfig();

		t_seal_type seal_type = (t_seal_type) oc.findObjById(seal_type_id,
				t_seal_type.class);
		ModelAndView mv = new ModelAndView("/jsp/seal/type/edit_form");
		mv.addObject("seal_type", seal_type);
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 印章种类修改
	 * 
	 * @param seal_type
	 *            印章种类信息
	 * @param seal_type_id
	 *            印章种类id
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(t_seal_type seal_type, String seal_type_id) {

		t_seal_type seal_typeDb = (t_seal_type) oc.findObjById(seal_type_id,
				t_seal_type.class);
		seal_typeDb.setMac_model(seal_type.getMac_model());
		seal_typeDb.setName(seal_type.getName());
		seal_typeDb.setNote(seal_type.getNote());
		seal_typeDb.setUser_id(seal_type.getUser_id());
		seal_typeDb.setConfig_ids(seal_type.getConfig_ids());
		oc.updateObj(seal_typeDb);
		logT.logH("修改印章种类", t_seal_type.class, seal_typeDb.get_id());
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 印章种类删除
	 * 
	 * @param seal_type_id
	 *            印章种类id
	 * @return
	 */
	@RequestMapping("/del")
	public ModelAndView del(String seal_type_id) {
		boolean flag = sealTypeConfigService.checkType(seal_type_id);
		if (flag) {
			oc.delObjById(seal_type_id, t_seal_type.class);
			logT.logH("删除印章种类", t_seal_type.class, seal_type_id);
			super.setTipMsg("删除成功", Tip.Type.success);
		} else {
			super.setTipMsg("删除失败,该类型有印章在用，或者该类型在申请中!", Tip.Type.error);
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	// 获取 type 下 的 config 的下拉 list
	/**
	 * 获取 type 下 的 config 的下拉 list
	 * 
	 * @param seal_type_id
	 *            印章类型id
	 * @return
	 */
	@RequestMapping("/configs_select")
	public ModelAndView configs(String seal_type_id) {

		t_seal_type type = oc.findObjById(seal_type_id, t_seal_type.class);
		List<t_seal_type_config> sealTypeConfigList = sealTypeConfigService
				.selectMyConfigList(type.getConfig_ids());

		ModelAndView mv = new ModelAndView("/jsp/seal/type/configs_select");
		mv.addObject("sealTypeConfigList", sealTypeConfigList);
		return mv;
	}
}